package file;

import common.Strings;

import java.util.ArrayList;

public class FileString {
    private File parent;
    private int lineNum;
    private String originalString;
    private int spaceShift;
    private String command;
    private String comment;

    private FileString(File parent, int lineNum, String inpString) {
        this.parent = parent;
        this.lineNum = lineNum;
        this.originalString = inpString;


        spaceShift = 0;
        command = "";
        comment = "";
        String tSring = originalString;
        while (spaceShift < tSring.length() && tSring.charAt(spaceShift) == ' ') spaceShift++;
        tSring = tSring.trim();
        int tCommentPos = tSring.indexOf("//");
        if (tCommentPos >= 0) {
            comment = tSring.substring(tCommentPos + 2).trim();
            command = tSring.substring(0, tCommentPos).trim();
        } else
            command = tSring.trim();
    }

    public File getParent() {
        return parent;
    }

    public int getLineNum() {
        return lineNum;
    }

    public String getOriginalString() {
        return originalString;
    }

    public String getTrimmedOriginalString() {
        return Strings.trinAndDeleteDoubleSpace(originalString);
    }

    public int getSpaceShift() {
        return spaceShift;
    }

    public void setSpaceShift(int spaceShift) {
        this.spaceShift = spaceShift;
    }

    public String getCommand() {
        return command;
    }

    public String getComment() {
        return comment;
    }

    public static ArrayList<FileString> getFileString(File parent, int lineNum, String inpString) {
        ArrayList<FileString> outFinalFileStrings = new ArrayList<FileString>();
        FileString baseFileSring = new FileString(parent, lineNum, inpString);
        if (baseFileSring.command.contains(";") &&
                !(baseFileSring.getTrimmedOriginalString().endsWith("}") && (baseFileSring.getTrimmedOriginalString().startsWith("~{") || baseFileSring.getTrimmedOriginalString().startsWith("~~{")))) {
            String[] splettedStrings = baseFileSring.command.split(";");
            for (String splettedString : splettedStrings) {
                FileString tFileString = new FileString(parent, lineNum, inpString);
                tFileString.command = splettedString;
                outFinalFileStrings.add(tFileString);
            }
        } else
            outFinalFileStrings.add(baseFileSring);
        return outFinalFileStrings;
    }

    public FileString getCopy() {
        FileString tFileString = new FileString(parent, lineNum, originalString);

        tFileString.parent = parent;
        tFileString.lineNum = lineNum;
        tFileString.originalString = originalString;
        tFileString.spaceShift = spaceShift;
        tFileString.command = command;
        tFileString.comment = comment;

        return tFileString;
    }
}
